package fr.auzaiphe.joueur.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;

import fr.auzaiphe.commun.Outils;
import fr.auzaiphe.joueur.controller.JoueurController;

/**
 *
 * @author raph
 *
 */
public class JoueurPanel extends JPanel
{
	private JoueurController controller;

	private SpringLayout layout;
	private JPanel panelChrono;
	private JLabel labelChrono;
	private SpringLayout layoutChrono;
	private JPanel panelLogin;
	private SpringLayout layoutLogin;
	private JPanel panelChat;
	private SpringLayout layoutChat;

	private JLabel labelUserName;
	private JTextField userNameField;
	private JLabel labelPassword;
	private JPasswordField passwordField;
	private JButton buttonConnect;

	private JTextArea areaChat;
	private JScrollPane scrollAreaChat;
	private JTextField fieldChat;
	private JButton buttonSend;


	public JoueurPanel( JoueurController pJoueurController )
	{
		/* Definition du panel général */
		super();

		this.controller = pJoueurController;

		this.layout = new SpringLayout();
		this.setLayout(this.layout);

		/* Définition des panels internes */
		this.panelChrono = new JPanel();
		this.layoutChrono = new SpringLayout();
		this.panelChrono.setLayout( this.layoutChrono );
		this.layout.putConstraint(SpringLayout.SOUTH, this.panelChrono, 160, SpringLayout.NORTH, this);
		this.layout.putConstraint(SpringLayout.NORTH, this.panelChrono, 5, SpringLayout.NORTH, this);
		this.layout.putConstraint(SpringLayout.WEST, this.panelChrono, 5, SpringLayout.WEST, this);
		this.layout.putConstraint(SpringLayout.EAST, this.panelChrono, -5, SpringLayout.EAST, this);
		this.add( this.panelChrono );

		this.panelLogin = new JPanel();
		this.layoutLogin = new SpringLayout();
		this.panelLogin.setLayout( this.layoutLogin );
		this.layout.putConstraint(SpringLayout.EAST, this.panelLogin, 333, SpringLayout.WEST, this);
		this.layout.putConstraint(SpringLayout.NORTH, this.panelLogin, 5, SpringLayout.SOUTH, this.panelChrono);
		this.layout.putConstraint(SpringLayout.WEST, this.panelLogin, 5, SpringLayout.WEST, this);
		this.layout.putConstraint(SpringLayout.SOUTH, this.panelLogin, -5, SpringLayout.SOUTH, this);
		this.add(this.panelLogin);

		this.panelChat = new JPanel();
		this.layoutChat = new SpringLayout();
		this.panelChat.setLayout( this.layoutChat );
		this.layout.putConstraint(SpringLayout.WEST, this.panelChat, 5, SpringLayout.EAST, this.panelLogin);
		this.layout.putConstraint(SpringLayout.NORTH, this.panelChat, 5, SpringLayout.SOUTH, this.panelChrono);
		this.layout.putConstraint(SpringLayout.EAST, this.panelChat, -5, SpringLayout.EAST, this);
		this.layout.putConstraint(SpringLayout.SOUTH, this.panelChat, -5, SpringLayout.SOUTH, this);
		this.add(this.panelChat);


		/* Definition des composants pour le chrono */
		this.labelChrono = new JLabel();
		this.labelChrono.setHorizontalAlignment(SwingConstants.CENTER);
		this.labelChrono.setFont(new Font("Calibri", Font.PLAIN, 120));
		this.labelChrono.setText("00:00");
		this.layoutChrono.putConstraint(SpringLayout.EAST, this.labelChrono, 5, SpringLayout.EAST, this.panelChrono);
		this.layoutChrono.putConstraint(SpringLayout.NORTH, this.labelChrono, 5, SpringLayout.NORTH, this.panelChrono);
		this.layoutChrono.putConstraint(SpringLayout.WEST, this.labelChrono, 5, SpringLayout.WEST, this.panelChrono);
		this.layoutChrono.putConstraint(SpringLayout.SOUTH, this.labelChrono, 5, SpringLayout.SOUTH, this.panelChrono);
		this.panelChrono.add(this.labelChrono);

		/* Definition des composants pour la connexion */
		this.labelUserName = new JLabel();
		this.labelUserName.setText("Nom de joueur");
		this.layoutLogin.putConstraint(SpringLayout.NORTH, this.labelUserName, 60, SpringLayout.NORTH, this.panelLogin);
		this.layoutLogin.putConstraint(SpringLayout.WEST, this.labelUserName, 30, SpringLayout.WEST, this.panelLogin);
		this.panelLogin.add(this.labelUserName);

		this.userNameField = new JTextField();
		this.layoutLogin.putConstraint(SpringLayout.NORTH, this.userNameField, 10, SpringLayout.SOUTH, this.labelUserName);
		this.layoutLogin.putConstraint(SpringLayout.WEST, this.userNameField, 0, SpringLayout.WEST, this.labelUserName);
		this.layoutLogin.putConstraint(SpringLayout.EAST, this.userNameField, -30, SpringLayout.EAST, this.panelLogin);
		this.panelLogin.add(this.userNameField);

		this.labelPassword = new JLabel();
		this.labelPassword.setText("Mot de passe");
		this.layoutLogin.putConstraint(SpringLayout.NORTH, this.labelPassword, 10, SpringLayout.SOUTH, this.userNameField);
		this.layoutLogin.putConstraint(SpringLayout.WEST, this.labelPassword, 0, SpringLayout.WEST, this.userNameField);
		this.panelLogin.add(this.labelPassword);

		this.passwordField = new JPasswordField();
		this.layoutLogin.putConstraint(SpringLayout.NORTH, this.passwordField, 10, SpringLayout.SOUTH, this.labelPassword );
		this.layoutLogin.putConstraint(SpringLayout.WEST, this.passwordField, 0, SpringLayout.WEST, this.labelPassword);
		this.layoutLogin.putConstraint(SpringLayout.EAST, this.passwordField, 0, SpringLayout.EAST, this.userNameField );
		this.panelLogin.add(this.passwordField);


		this.buttonConnect = new JButton("Connexion");
		this.layoutLogin.putConstraint(SpringLayout.NORTH, this.buttonConnect, 10, SpringLayout.SOUTH, this.passwordField );
		this.layoutLogin.putConstraint(SpringLayout.EAST, this.buttonConnect, 0, SpringLayout.EAST, this.passwordField );
		this.buttonConnect.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if ( JoueurPanel.this.controller.getPartieEnCours() )
				{
					JoueurPanel.this.controller.sendConnexion( JoueurPanel.this.userNameField.getText(),
							   new String(JoueurPanel.this.passwordField.getPassword()) );
				}
				else
				{
					JOptionPane.showMessageDialog( null, "La partie doist être lancée avant que vous puissiez vous connecter.", "Erreur Connexion", JOptionPane.ERROR_MESSAGE );
				}
			}
		});
		this.panelLogin.add(this.buttonConnect);

		/* Definition des composants du chat */
		this.buttonSend = new JButton("Envoyer");
		this.layoutChat.putConstraint(SpringLayout.SOUTH, this.buttonSend, -5, SpringLayout.SOUTH, this.panelChat );
		this.layoutChat.putConstraint(SpringLayout.EAST, this.buttonSend, -5, SpringLayout.EAST, this.panelChat);
		this.buttonSend.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				JoueurPanel.this.controller.sendMessage( JoueurPanel.this.fieldChat.getText() );
				JoueurPanel.this.fieldChat.setText( null );
			}
		});
		this.panelChat.add( this.buttonSend );

		this.fieldChat = new JTextField();
		this.layoutChat.putConstraint(SpringLayout.SOUTH, this.fieldChat, 0, SpringLayout.SOUTH, this.buttonSend );
		this.layoutChat.putConstraint(SpringLayout.WEST, this.fieldChat, 5, SpringLayout.WEST, this.panelChat );
		this.layoutChat.putConstraint(SpringLayout.EAST, this.fieldChat, -5, SpringLayout.WEST, this.buttonSend );
		this.layoutChat.putConstraint(SpringLayout.NORTH, this.fieldChat, 0, SpringLayout.NORTH, this.buttonSend );
		this.panelChat.add( this.fieldChat );

		this.areaChat = new JTextArea();
		this.scrollAreaChat = new JScrollPane( this.areaChat );
		this.areaChat.setEditable( false );
		this.areaChat.setLineWrap( true );
		this.areaChat.setBackground( Color.white );
		this.layoutChat.putConstraint(SpringLayout.NORTH, this.scrollAreaChat, 5, SpringLayout.NORTH, this.panelChat );
		this.layoutChat.putConstraint(SpringLayout.WEST, this.scrollAreaChat, 5, SpringLayout.WEST, this.panelChat );
		this.layoutChat.putConstraint(SpringLayout.SOUTH, this.scrollAreaChat, -5, SpringLayout.NORTH, this.fieldChat);
		this.layoutChat.putConstraint(SpringLayout.EAST, this.scrollAreaChat, -5, SpringLayout.EAST, this.panelChat);
		this.panelChat.add( this.scrollAreaChat );
	}

	public JTextArea getChat()
	{
		return this.areaChat;
	}

	public void setTime(int pSecondesRestantes)
	{
		this.labelChrono.setText( Outils.secondToHMinSec(pSecondesRestantes) );
	}
}
